// pages/todayAppointments/todayAppointments.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    username: '',
    assistant: [],
    scrollHeight: 0,
    currentId: -1,
    assistant1: [{
      logoLink: '/images/defaultImage.png',
      assistantName: '小红',
      assistantTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      assistantName: '小明',
      assistantTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      assistantName: '小白',
      assistantTel: "18727508188"
    },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();

    this.getUnresAssistant()
    this.setData({
      username: getApp().globalData.username,
    })
    try {
      var ap = wx.getStorageSync('unResAssistant')
      this.setData({
        assistant: ap
      })
    } catch(e) { }

    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.getUnresAssistant()

    this.setData({
      username: getApp().globalData.username,
    })
    try {
      var ap = wx.getStorageSync('Assistant')
      this.setData({
        assistant: ap
      })
    } catch(e) { }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUnresAssistant()
  },

  /**
   * 获取未负责此医生的助理信息
   */
  getUnresAssistant: function () {
    var that = this
    var id = this.data.username
    
    wx.request({
      url: that.data.url + 'getUnresAssistantServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorID: id
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data != "空") {
          try {
            wx.setStorageSync("Assistant", res.data)
          } catch (e) { }
          that.setData({
            assistant: res.data
          })
        }
        else if(res.data == "空") {
          that.setData({
            assistant: null
          })
        }
        else {
          wx.showToast({
            title: '获取失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络连接失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 医生确认添加助理
   */
  submitBtn: function(e) {
    var that = this
    var id = e.currentTarget.dataset.idx
    var assistantTel = this.data.assistant[id].assistantTel

    wx.request({
      url: getApp().globalData.url + 'doctorSetAssistant',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorTel: getApp().globalData.username,
        assistantTel: assistantTel,
      },
      success(res) {
        if(res.data == "Full")
        {
          wx.showToast({
            title: '最多添加三名助理',
            icon: 'none',
          })
        }
        else
        {
          wx.showToast({
            title: '已成功添加该助理',
            icon: 'success',
          })
          that.getUnresAssistant()
        }
      },
      fail(res) {
        wx.showToast({
          title: '助理添加失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
})